// pages/order/order.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import { get } from "../../utils/api";
Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 步进器默认值
        Value:0,
        // 订单
        orderList:[],
         //搜索栏值
        indexValue:'',
        // 左侧选中导航索引
        activeKey:0,  
        //规格的显示与隐藏
        showssf:false, 
        // 菜品信息 
        foodMessage:'',
        //重量弹出与隐藏
        showWeight:false, 
        //重量总价
        weightPrice:0, 
        ids:'cc',
        //菜品列表数据
        dishList:[{
        dishName:'川菜',
        Ids:'cc',
        menulist:[{
            menuname:'水煮肉片',
            id:2,
            price:100,
            standards: 0,
            num:0,
            showssf:false,
            weightPrice:0,
            numPrice:0,
            weight:0
            

        },{
            menuname:'麻婆豆腐',
            id:3,
            price:50,
            standards: 1,
            weightPrice:0,
            numPrice:0,
            num:0,
            showssf:false,
            weight:0
            
        },{
            menuname:'宫保鸡丁',
            id:4,
            price:80,
            standards: 1,
            num:0,
            showssf:false,
            weightPrice:0,
            numPrice:0,
            weight:0
            
        },{
            menuname:'鱼香肉丝',
            id:5,
            price:70,
            standards: 1,
            showssf:false,
            num:0,
            weightPrice:0,
            numPrice:0,
            weight:0
        
            }]
        },{
            dishName:'湘菜',
            
            Ids:'xc',
            menulist:[{
                menuname:'水煮肉片',
                id:11,
                price:100,
                standards: 1,
                showssf:false,
                num:0,
                weightPrice:0,
                numPrice:0,
                weight:0
                
            },{
                menuname:'麻婆豆腐',
                id:12,
                price:50,
                standards: 0,
                showssf:false,
                num:0,
                weightPrice:0,
                numPrice:0,
                weight:0
            },{
                menuname:'宫保鸡丁',
                id:13,
                price:80,
                standards: 1,
                showssf:false,
                num:0,
            weightPrice:0,
            numPrice:0,
            weight:0
            },{
                menuname:'鱼香肉丝',
                id:14,
                price:70,
                standards: 3,
                showssf:false,
                num:0,
                weightPrice:0,
                numPrice:0,
                weight:0
            }]
        },],
        message:{
        },
        spcf:{
            way: [{
                    txt: '常规',
                    price: 0
              },{
                    txt: '加辣',
                    price: 10
                },
                {
                    txt: '加麻',
                    price: 10
                },
                {
                    txt: '加糖',
                    price: 10
                },
                {
                    txt: '加醋',
                    price: 10
                }
                ],
    size:[
            {txt:'小份',price: 0.5},
            {txt:'中份',price:1},
            {txt:'大份',price:1.5}
                ]
        },
    // 合计
    totalPrice:0
},

    
    // 侧边导航点击
    onSidebarChange({target:{dataset:{ids}}}){

        this.setData({ids})
    },

    

    // 选重量
    closeWeight({target:{dataset:{id,way}}}){

        this.setData({showWeight: true})
        
        let message = {}
        this.data.dishList.forEach(i=>{
            i.menulist.some(item =>{
                    if (item.id === id) {
                        message = item
                        return true
                    }   
                })   
            })
        this.setData({message})
    },

    // 计算重量总价
    weightPrice({detail:{value},target:{dataset:{price}}}){
        console.log(value * price);
        this.setData({weightPrice :value * price})
    },

    // 取消选择重量
    weightCancel(){
        this.setData({showWeight:false,})
    },

    // 确认选择重量
    weightConfirm({target:{dataset:{id,price,showssf}}}){
        this.setData({showWeight:false})
        this.setmath(id,'weightPrice',this.data.weightPrice)
        this.setmath(id,'weight',this.data.weightPrice / price)
        this.setmath(id,showssf,true)
        
    },

    // 修改数量
    onChange({detail,target:{dataset:{id}}}){
        let message = {}
        this.data.dishList.forEach(i=>{
            i.menulist.some(item =>{
                    if (item.id === id) {
                        message = item
                        return true
                    }   
                })   
            })
            this.setData({message})
        this.setmath(id,'num',detail)
 
        detail >0 ? this.setmath(id,'showssf',true): this.setmath(id,'showssf',false)
       if( detail ===0)  {
        this.setData({message:{}})
        this. setmath(id,'spec',0)
        this. setmath(id,'tastes',0)
       } 
    },

    // 规格遮罩层关闭
    onPopupClose(){
        this.setData({
            showssf:false
        })
    },

    // 修改数据
    setmath(id,way,value){
      const dishList =  this.data.dishList
      dishList.forEach(i=>{
        i.menulist.some(item =>{
                if (item.id === id) {
                    item[way] = value
                    return true
                }   
            })   
        })

        this.setData({dishList})
        this.addHandler(id)
    },

    // 取消选择规格
    onClose(){
        this.setData({showssf:false})
    },

    // 确认规格
    specHandle({detail:{size,way}}){{      
        const {id } =this.data.message
         
        this.setmath(id ,'spec',size)
        this.setmath(id ,'tastes',way)
        this.setData({showssf:false})
       
    }},
    
    // 获取做法和规格
    async specificationHandler({currentTarget:{dataset:{id}}}){
        const {data} = await get(`http://192.168.35.43:82/menu/findById?id=${id}`)
      
        let message = {}
        this.data.dishList.forEach(i=>{
            i.menulist.some(item =>{
                    if (item.id === id) {
                        message = item
                        return true
                    }   
                })   
        })
       
        this.setData({message,showssf:true,spcf:data.data})
        
    },

    // 添加事件
    addHandler(ID){
        let {orderList,dishList} = this.data
        let itm = null
        dishList.forEach(i=>{
            i.menulist.some(item =>{
                    if (item.id === ID) {
                         itm =item
                        return true
                    }   
                })   
            })
        const {weight,weightPrice, showssf,num,menuname,id,standards,spec=0,tastes=0,price}  =itm 
        
        if (num ===0 && weight ===0) {
            orderList.some((i,idx,arr) =>{
                return  i.id === id && arr.splice(idx,1)
              })
              this.setData({orderList})
              this.getTotalPrice()
              return
        }
        const addOk =  orderList.some((i,idx,arr) =>{
            return  i.id === id && arr.splice(idx,1)
          })
        const numPrice = num * price
        const sizePrice = num * (spec.price * price + (tastes.price) )
        const foodsitem = {
            menuname,
            id,
            price,
            standards,
            showssf,
            sizePrice,
            spec,
            tastes,
            weight,
            num,
            weightPrice,
            numPrice,
        }
         orderList = [foodsitem,...orderList]
        this.setData({orderList})
        this.getTotalPrice()
    },

    setHeight() {
        const { windowHeight } = wx.getSystemInfoSync();
        
        // 设置内容区域高度。
        this.setData({
            viewHeight: windowHeight - 90
        });
      },

      getTotalPrice(){
          let totalPrice =0
          this.data.orderList.forEach(i =>{
             
            totalPrice +=  (i.weightPrice || (i.sizePrice ? 0:i.numPrice)) + (i.sizePrice||0) +(i.tastes.price||0)
          })
          this.setData({totalPrice})
      },

      getOrderNum(id){
        Dialog.confirm({
            message: '是否下单',
          })
            .then( async () => {
                wx.switchTab({
                    url: `/pages/pay/pay`,
                  })
        const  deedNum = wx.getStorageSync('deedNum')   
        const{data} = await get(`http://192.168.35.23:82/order/getOrder`)
        console.log(data);
        
         wx.setStorageSync('order', this.data.orderList)
         wx.setStorageSync('totalPrice', this.data.totalPrice)
         wx.setStorageSync('orderNum',data)
    
          })
            .catch(() => {
            });
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLoad: function (options) {
        this.setHeight()
        this.getdata()
    },

    onReady: function () {
    },

    async getdata(){
        const { data} = await get('http://192.168.35.43:82/menu/findAll')
        console.log(data);
        this.setData({dishList :data.data})
    },
    /**
     * 生命周期函数--监听页面显示
     */
    
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },
 
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})